import Api from "@/api/api-declare";
import type { ApiLinesTypes } from "@/api/api-types/api-lines-types";

export function useLines() {
	const query = ref<ApiLinesTypes.GetLinesReq>({
		bueiness: 1,
		area: 1, //是否获取区域数据 0否 1是
		status: 0, //获取全部:-1;获取关闭的线路:status=0;获取开启的线路status=1
		need_order_total: 0, //是否需要获取订单总数 0否 1是
		need_wait_accept_order_total: 0, //是否需要线路待接单订单数。0否 1是
		last_request_time: 0, //上次请求时间(根据这个时间获取之后订单的增量)。当need_wait_accept_order_total>0时必传
	});

	const lines = ref<ApiLinesTypes.GetLinesResLineItem[]>([]);

	const getLines = (): Promise<void> => {
		return new Promise((r, j) => {
			Api.ApiLines.getLines(query.value).then(res => {
				lines.value = [...lines.value, ...res.list];
				r();
			}).catch(j);
		});
	};

	const getLineDetail = (line_id : number) : Promise<ApiLinesTypes.GetLinesResLineItem> => {
		return new Promise((r, j) => {
			Api.ApiLines.lineDetail({
				line_id
			}).then(res => {
				r(res.info);
			}).catch(j);
		});
	};

	return {
		query,
		lines,
		getLines,
		getLineDetail
	};
};